package com.wzl.demo_08_03;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

import androidx.annotation.Nullable;

public class MyService extends IntentService {
    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    //继承IntentService必须实现构造方法
    public MyService() {
        super("MyService");//线程名
    }

    //继承IntentService必须重写onHandleIntent方法
    //onHandleIntent方法中执行耗时操作
    //onHandleIntent方法中不能调用stopSelf()方法
    //onHandleIntent方法中不能调用startService()方法
    //onHandleIntent方法中不能调用bindService()方法
    //onHandleIntent方法中不能调用unbindService()方法
    //创建工作队列，用于将一个Intent逐一传递给onHandleIntent()实现，这样您就永远不必担心多线程问题。
    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Log.i("MyService", "onHandleIntent");
        Log.i("MyService thread", Thread.currentThread().getName());

        for(int i=1;i<=10;i++){
            Log.i("test","i:"+i);
            try {
                Thread.sleep(1000);
            }catch(InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.stopSelf();//结束服务
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("MyService", "onCreate");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("MyService", "onDestroy");
    }
}
